<?php  
	/* 
	    Plugin Name: WP-TimeTracking
	    Plugin URI: http://www.mintel.it 
	    Description: Plugin for timeTracking: Include in your post the following string <!--wpTimeTracking--> Currentlty show the event correctly only. Cannot be added, removed, updated or inserted.
	    Author: Luca Preziati, Fabio Trezzi
	    Version: 0.6a 
	    Author URI: http://www.mintel.it
    */  



include_once 'view.php';
include_once '\lib\FirePHPCore\fb.php';
include_once '\lib\FirePHPCore\FirePHP.class.php';





if( !class_exists( 'WP_TimeTracking' ) ) {
	
	class WP_TimeTracking {
		var $is_installed = false;
		var $view;
			
		function WP_TimeTracking (){
			$this-> view = new View($_REQUEST,$_SERVER);
			
			
			//TODO:: da rimuovere il commento 
	/*		if( get_option( 'WP-TimeTracking Version' ) !== FALSE ) {
				$this->is_installed = true;
			}
	*/
		}
			
		/**
		 * Adds all additional pages necessary for the correct administration of WP-Project, as well as enqueueing any
		 * JavaScript files necessary for those files.
		 */
		function on_admin_menu() {
			$this->page_slugs[ 'top_level' ] = add_menu_page( 'Wp-TimeTracking', 'tracking', 8, 'wp-timetracking', array( &$this, 'wp_calendar_config' ) );
		/*	if( $this->is_installed ) {
				$this->page_slugs[ 'timeTracking' ]  = add_submenu_page( 'wp-timetracking', 'tracking', 'tracking', 8, 'wp-timetracking/calendar', array( &$this, 'wp_calendar' ) );
			}*/
		}
		
		function on_admin_head() {
		}

		function on_header(){
			?>
				<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" type="text/css" />
			<?php 
		}
		
		/**
		 * Perform initialization required after WordPress loads but before any HTTP headers
		 * are sent.
		 */
		function on_init() {	
			$this->is_installed = true;
			//TODO:: da rimuovere
		}
		
		function on_activate() {
		}
		
		/**
		 * This function will not make any changes to data that exists in the database.  That is reserved for the 
		 * uninstall_data function.  For now, this is just a placeholder in case some action becomes necessary
		 * on deactivation.
		 */
		function on_deactivate() {
			// We're not really doing anything on a deactivation, because everything is being uninstalled
			// through a separate mechanism to ensure none of the good data gets erased.
		}
		
		function install(){
			

		}
		
		function wp_calendar(){
			//wp_enqueue_script("jquery");
			//wp_enqueue_script("jquery-ui-core");
			//wp_enqueue_script("jquery-ui-dialog");
			echo "<h1>your activity calendar</h1>";
			ob_start();
			$userId=get_current_user_id();
			fb($_REQUEST,'request');
			fb($_SERVER,'server');
			$html = $this-> view->modalPanelJQuery($userId);
			fb($userId,'user Id');
			$html.="<div>";
			$html.= $this-> view ->getOwnCalendar($userId);
			$html.="</div>";
			echo $html;

		}
		
		function wp_calendar_config(){
			?>
				<h1>Configuration of plugin</h1>
			
			<?php 
		}
		
		function calendar(){
			$text = str_replace('<!--wpTimeTracking-->',$this->wp_calendar(),$text);
			return $text;
		}
		
		function action($action,$request){
			fb($action,'action');
			$controller = $this->view->controller;
			switch ($action){
				case 0: //insert action
					fb('action 0');	
					$controller->insert($request);
					break;
				case 1: //update action
					//update
					fb('action 1');
					break;
				case 2:	//delete action
					fb('action 2');
					fb($request['eventId'],'eventid');
					$controller->deletePerId($request['eventId']);
					break;
				case 3: //get event action
					fb('action 3');
					break;
				default:
					fb('action not 1,2,3');
			}
		}
		
	} 

}



if( class_exists( 'WP_TimeTracking' ) ) {

	$wp_calendar = new WP_TimeTracking();
	
	register_activation_hook( __FILE__, array( &$wp_calendar, 'on_activate' ) );
	register_deactivation_hook( __FILE__, array( &$wp_calendar, 'on_deactivate' ) );
	fb($_REQUEST,'request');
	
	// Actions
	add_action( 'admin_menu', array( &$wp_calendar, 'on_admin_menu' ) );
	add_action( 'init', array( &$wp_calendar, 'on_init' ) );
	add_filter('the_content',array(&$wp_calendar,'calendar'));
	
	if(isset($_REQUEST['formAction'])) {
		$action = $_REQUEST['formAction'];
		$wp_calendar->action($action,$_REQUEST);
	}
		
}
?>